#include "publicswidget.h"
#include "ui_publicswidget.h"

#include <QMessageBox>
#include "todoeditor.h"
PublicsWidget::PublicsWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::PublicsWidget)
{
    ui->setupUi(this);

    connect (ui->chkIsActive, SIGNAL(toggled(bool)), ui->grpDates, SLOT(setEnabled(bool)));
    connect (ui->chkIsRecurring, SIGNAL(toggled(bool)), ui->grpIsRecurring, SLOT(setEnabled(bool)));
}

PublicsWidget::~PublicsWidget()
{
    delete ui;
}

void PublicsWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void PublicsWidget::LoadData()
{
    ui->lblRef->setText(RefNumber);
    QString query = tr("SELECT * FROM publics WHERE RefNumber = '%1'").arg(RefNumber);
    QSqlQuery *qu = new QSqlQuery(query, db);
    while (qu->next())
    {
        ui->txtDescription->setText(qu->value(2).toString());
        ui->txtNotes->setText(qu->value(3).toString());
        ui->txtStartDate->setDate(qu->value(4).toDate());
        ui->txtEndDate->setDate(qu->value(5).toDate());
        ui->chkIsActive->setChecked(qu->value(6).toBool());
        ui->chkIsRecurring->setChecked(qu->value(7).toBool());
        QString RecurringMode = qu->value(8).toString();
       // QMessageBox::information(this, "", RecurringMode);
        if (RecurringMode == "Daily")
            ui->optDaily->setChecked(true);
        if(RecurringMode == "Weekly")
            ui->optWeekly->setChecked(true);
        if (RecurringMode == "Monthly")
            ui->optMonthly->setChecked(true);
        if (RecurringMode == "Yearly")
            ui->optYearly->setChecked(true);
        model = new QSqlQueryModel(ui->trvToDo);
        model->setQuery(tr("SELECT * FROM todolist WHERE `Ref #` = '%1'").arg(RefNumber));
        ui->trvToDo->setModel(model);
        ui->trvToDo->setAlternatingRowColors(true);
        ui->trvToDo->setColumnHidden(0, true);
    }
}

void PublicsWidget::SaveData()
{
    QString testquery = tr("SELECT * FROM publics WHERE RefNumber = '%1'").arg(RefNumber);
    QSqlQuery *testqu = new QSqlQuery(testquery, db);

    QString isActive = "0";
    QString isRecurring = "0";
    QString RecurringMode = "Daily";
    QString startDate = ui->txtStartDate->text();
    QString endDate = ui->txtEndDate->text();
    if (ui->chkIsActive->isChecked())
        isActive = "1";
    if (ui->chkIsRecurring->isChecked())
    {
        isRecurring = "1";
        if (ui->optDaily->isChecked())
            RecurringMode = "Daily";

        if (ui->optMonthly->isChecked())
            RecurringMode = "Monthly";

        if (ui->optWeekly->isChecked())
            RecurringMode = "Weekly";

        if (ui->optYearly->isChecked())
            RecurringMode = "Yearly";
    }

    if (testqu->size() > 0)
    {

        QString query = tr("UPDATE publics SET Description = '%1', Notes = '%2', StartDate = '%4',"
                           " EndDate = '%5', IsActive = '%6', IsRecurring = '%7', RecurringMode = '%8'"
                           " WHERE RefNumber = '%3'")
                        .arg(ui->txtDescription->toPlainText(),
                             ui->txtNotes->toPlainText(), RefNumber, startDate, endDate, isActive, isRecurring, RecurringMode);
        //QMessageBox::information(this, "Query", query);
        QSqlQuery *qu = new QSqlQuery(query, db);
        qu->clear();
    }
    else
    {
        QString insertQuery = tr("INSERT INTO publics(RefNumber, Description, Notes, StartDate, EndDate, IsActive, "
                                 " IsRecurring, RecurringMode)"
                                 " VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8' )").arg(
                RefNumber, ui->txtDescription->toPlainText(), ui->txtNotes->toPlainText()
                , startDate, endDate, isActive, isRecurring, RecurringMode
                );
        QSqlQuery *insQu = new QSqlQuery(insertQuery, db);
        insQu->clear();
    }
}

void PublicsWidget::on_cbdAddToDo_clicked()
{
    ToDoEditor ed(this);
    ed.RefNo = RefNumber;
    ed.state = "New";
    ed.showData();
    if (ed.exec())
        LoadData();
}

void PublicsWidget::on_cmdEditToDo_clicked()
{
    QString ToDoID = model->record(ui->trvToDo->currentIndex().row()).value(0).toString();
    ToDoEditor ed(this);
    ed.ToDoID = ToDoID;
    ed.RefNo = RefNumber;
    ed.state = "Edit";
    ed.showData();
    if (ed.exec())
        LoadData();
}
